package dbutil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

/*******************************************************************************
 * Filename : DBUtils.java Decription : Chứa các tiện ích cho Database Created
 * date : Jun 2, 2008 Author : Phonglt
 ******************************************************************************/
public class DBUtils {

	/***************************************************************************
	 * Method name : getRowData Return type : Object[] Decription : lấy 1 DÒNG
	 * dữ liệu ra mảng từ ResultSet Created date : May 2, 2008 Author : Honglk
	 **************************************************************************/
	public static Object[] getRowData(ResultSet resultSet) throws SQLException {
		ResultSetMetaData rsMeta = resultSet.getMetaData();
		int columnCount = rsMeta.getColumnCount();
		Object rowData[] = new Object[columnCount];
		for (int colIndex = 1; colIndex <= columnCount; colIndex++) {
			rowData[colIndex - 1] = resultSet.getObject(colIndex);
		}
		return rowData;
	}

	/***************************************************************************
	 * Method name : getColumnNames Return type : String[] Decription : lấy ra
	 * tên cột từ ResultSet Created date : May 2, 2008 Author : Honglk
	 **************************************************************************/
	public static String[] getColumnNames(ResultSet resultSet)
			throws SQLException {
		ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
		int columnCount = resultSetMetaData.getColumnCount();
		String columnNames[] = new String[columnCount];
		for (int colIndex = 1; colIndex <= columnCount; colIndex++) {
			columnNames[colIndex - 1] = resultSetMetaData
					.getColumnName(colIndex);
		}

		return columnNames;
	}

	/***************************************************************************
	 * Method name : putDataIntoList Return type : ArrayList<Map<String,
	 * Object>> Decription : lấy tất cả dữ liệu trong ResultSet ra List of Map
	 * Created date : May, 2008 Author : Honglk Modify 1 : Phonglt (Jun 2, 2008)
	 **************************************************************************/
	public static ArrayList<HashMap<String, Object>> putDataIntoList(
			ResultSet resultSet) {
		ArrayList<HashMap<String, Object>> listData = new ArrayList<HashMap<String, Object>>();
		try {
			while (resultSet.next()) {
				Object[] data = getRowData(resultSet);
				String[] columnName = getColumnNames(resultSet);
				HashMap<String, Object> map = new HashMap<String, Object>();
				for (int i = 0; i < columnName.length; i++) {
					map.put(columnName[i], data[i]);
				}
				listData.add(map);
			}
		} catch (SQLException ex) {

		}
		return listData;
	}

	public static int deleteData(String tableName, String column, String value) {
		ConnectDB.connectDB();
		String SQL = "DELETE FROM " + tableName + " WHERE " + column + "='"
				+ value + "'";
		int numberRowsDeleted = -1;
		try {
			numberRowsDeleted = ConnectDB.statement.executeUpdate(SQL);
		} catch (SQLException se) {
			System.out.println(" SQL is invalid");
			se.printStackTrace();
		}

		return numberRowsDeleted;

	}
	
	

	public static void main(String[] args) {
		try{
			ConnectDB.connectDB();
		PreparedStatement preStat = ConnectDB.connect
				.prepareStatement("UPDATE thanhvien SET name=?,diachi=?,mail=?,phone=?,hoten=?"
						+ " WHERE id='" +18 + "'");
		preStat.setString(1, "hongnonglam");
		preStat.setString(2, "phu yen");
		preStat.setString(3, "hongnf");
		preStat.setString(4, "00894058");
		preStat.setString(5, "le kimhong");
		preStat.executeUpdate();
		}catch( SQLException e){
			e.printStackTrace();
		}
	}

}
